home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch6 / BinCont2.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-04-25  |  10.5 KB  |  318 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
  3. Begin VB.Form frmBinCont2 
  4.    Caption         =   "BinCont2 []"
  5.    ClientHeight    =   4425
  6.    ClientLeft      =   165
  7.    ClientTop       =   735
  8.    ClientWidth     =   9120
  9.    LinkTopic       =   "Form2"
  10.    ScaleHeight     =   4425
  11.    ScaleWidth      =   9120
  12.    StartUpPosition =   3  'Windows Default
  13.    Begin VB.PictureBox picHistogram 
  14.       Height          =   1455
  15.       Index           =   2
  16.       Left            =   6120
  17.       ScaleHeight     =   93
  18.       ScaleMode       =   3  'Pixel
  19.       ScaleWidth      =   188
  20.       TabIndex        =   4
  21.       Top             =   0
  22.       Width           =   2880
  23.    End
  24.    Begin VB.PictureBox picHistogram 
  25.       Height          =   1455
  26.       Index           =   1
  27.       Left            =   3120
  28.       ScaleHeight     =   93
  29.       ScaleMode       =   3  'Pixel
  30.       ScaleWidth      =   188
  31.       TabIndex        =   3
  32.       Top             =   0
  33.       Width           =   2880
  34.    End
  35.    Begin MSComDlg.CommonDialog dlgOpenFile 
  36.       Left            =   0
  37.       Top             =   840
  38.       _ExtentX        =   847
  39.       _ExtentY        =   847
  40.       _Version        =   393216
  41.    End
  42.    Begin VB.PictureBox picHistogram 
  43.       Height          =   1455
  44.       Index           =   0
  45.       Left            =   120
  46.       ScaleHeight     =   93
  47.       ScaleMode       =   3  'Pixel
  48.       ScaleWidth      =   188
  49.       TabIndex        =   2
  50.       Top             =   0
  51.       Width           =   2880
  52.    End
  53.    Begin VB.PictureBox picOriginal 
  54.       AutoSize        =   -1  'True
  55.       Height          =   2775
  56.       Left            =   120
  57.       ScaleHeight     =   181
  58.       ScaleMode       =   3  'Pixel
  59.       ScaleWidth      =   157
  60.       TabIndex        =   1
  61.       Top             =   1560
  62.       Width           =   2415
  63.    End
  64.    Begin VB.PictureBox picResult 
  65.       Height          =   2775
  66.       Left            =   2640
  67.       ScaleHeight     =   181
  68.       ScaleMode       =   3  'Pixel
  69.       ScaleWidth      =   157
  70.       TabIndex        =   0
  71.       Top             =   1560
  72.       Width           =   2415
  73.    End
  74.    Begin VB.Menu mnuFile 
  75.       Caption         =   "&File"
  76.       Begin VB.Menu mnuFileOpen 
  77.          Caption         =   "&Open..."
  78.          Shortcut        =   ^O
  79.       End
  80.       Begin VB.Menu mnuFileSaveAs 
  81.          Caption         =   "Save &As..."
  82.          Shortcut        =   ^A
  83.       End
  84.    End
  85. Attribute VB_Name = "frmBinCont2"
  86. Attribute VB_GlobalNameSpace = False
  87. Attribute VB_Creatable = False
  88. Attribute VB_PredeclaredId = True
  89. Attribute VB_Exposed = False
  90. Option Explicit
  91. Private MinIndex(0 To 2) As Integer
  92. Private MaxIndex(0 To 2) As Integer
  93. ' Arrange the controls.
  94. Private Sub ArrangeControls()
  95. Dim wid As Single
  96.     ' Position the result PictureBox.
  97.     picResult.Move _
  98.         picOriginal.Left + picOriginal.Width + 120, _
  99.         picOriginal.Top, _
  100.         picOriginal.Width, _
  101.         picOriginal.Height
  102.     picResult.Cls
  103.     ' This makes the image resize itself to
  104.     ' fit the picture.
  105.     picResult.Picture = picResult.Image
  106.     ' Make the form big enough.
  107.     wid = picResult.Left + picResult.Width
  108.     If wid < picHistogram(2).Left + picHistogram(2).Width Then _
  109.         wid = picHistogram(2).Left + picHistogram(2).Width
  110.     Width = wid + Width - ScaleWidth + 120
  111.     Height = picResult.Top + picResult.Height + _
  112.         Height - ScaleHeight + 120
  113.     DoEvents
  114. End Sub
  115. ' Transform the image.
  116. Private Sub TransformImage(ByVal Index As Integer, ByVal cutoff As Single)
  117. Dim pixels() As RGBTriplet
  118. Dim bits_per_pixel As Integer
  119. Dim test_value As Byte
  120. Dim X As Integer
  121. Dim Y As Integer
  122.     ' Get the pixels from picOriginal.
  123.     GetBitmapPixels picOriginal, pixels, bits_per_pixel
  124.     ' Set the pixel color values.
  125.     For Y = 0 To picOriginal.ScaleHeight - 1
  126.         For X = 0 To picOriginal.ScaleWidth - 1
  127.             With pixels(X, Y)
  128.                 Select Case Index
  129.                     Case 0
  130.                         test_value = .rgbRed
  131.                     Case 1
  132.                         test_value = .rgbGreen
  133.                     Case 2
  134.                         test_value = .rgbBlue
  135.                 End Select
  136.                 If test_value >= cutoff Then
  137.                     .rgbRed = 255
  138.                     .rgbGreen = 255
  139.                     .rgbBlue = 255
  140.                 Else
  141.                     .rgbRed = 0
  142.                     .rgbGreen = 0
  143.                     .rgbBlue = 0
  144.                 End If
  145.             End With
  146.         Next X
  147.     Next Y
  148.     ' Set picResult's pixels.
  149.     SetBitmapPixels picResult, bits_per_pixel, pixels
  150.     picResult.Picture = picResult.Image
  151. End Sub
  152. ' Show the component histograms.
  153. Private Sub ShowHistograms(ByVal picImage As PictureBox)
  154. Dim counts(0 To 2, 0 To 255) As Long
  155. Dim max_count As Long
  156. Dim brightness As Integer
  157. Dim pixels() As RGBTriplet
  158. Dim bits_per_pixel As Integer
  159. Dim X As Integer
  160. Dim Y As Integer
  161. Dim i As Integer
  162. Dim j As Integer
  163.     ' Clear the previous results.
  164.     For i = 0 To 2
  165.         picHistogram(i).Line _
  166.             (picHistogram(i).ScaleLeft, picHistogram(i).ScaleTop)- _
  167.             Step(picHistogram(i).ScaleWidth, picHistogram(i).ScaleHeight), _
  168.             picHistogram(i).BackColor, BF
  169.         picHistogram(i).Refresh
  170.     Next i
  171.     ' Get the pixels from picImage.
  172.     GetBitmapPixels picImage, pixels, bits_per_pixel
  173.     ' Count the brightness values.
  174.     For Y = 0 To picImage.ScaleHeight - 1
  175.         For X = 0 To picImage.ScaleWidth - 1
  176.             With pixels(X, Y)
  177.                 counts(0, .rgbRed) = counts(0, .rgbRed) + 1
  178.                 counts(1, .rgbGreen) = counts(1, .rgbGreen) + 1
  179.                 counts(2, .rgbBlue) = counts(2, .rgbBlue) + 1
  180.             End With
  181.         Next X
  182.     Next Y
  183.     ' Find the largest count value.
  184.     For i = 0 To 2
  185.         ' Skip value 0. There tend to be a lot of
  186.         ' them and they dominate things.
  187.         For j = 1 To 255
  188.             If max_count < counts(i, j) _
  189.                 Then max_count = counts(i, j)
  190.         Next j
  191.     Next i
  192.     ' Display the brightness histograms.
  193.     For i = 0 To 2
  194.         picHistogram(i).ScaleTop = 1.1 * max_count
  195.         picHistogram(i).ScaleHeight = -1.2 * max_count
  196.         picHistogram(i).ScaleLeft = -1
  197.         picHistogram(i).ScaleWidth = 258
  198.         For brightness = 0 To 255
  199.             If counts(i, brightness) > 0 Then _
  200.                 picHistogram(i).Line (brightness, 0)-(brightness + 1, counts(i, brightness)), , BF
  201.         Next brightness
  202.         picHistogram(i).Picture = picHistogram(i).Image
  203.     Next i
  204. End Sub
  205. ' Start in the current directory.
  206. Private Sub Form_Load()
  207. Dim i As Integer
  208.     picOriginal.AutoSize = True
  209.     picOriginal.ScaleMode = vbPixels
  210.     picOriginal.AutoRedraw = True
  211.     picResult.ScaleMode = vbPixels
  212.     picResult.AutoRedraw = True
  213.     For i = 0 To 2
  214.         picHistogram(i).AutoRedraw = True
  215.     Next i
  216.     dlgOpenFile.CancelError = True
  217.     dlgOpenFile.InitDir = App.Path
  218.     dlgOpenFile.Filter = _
  219.         "Bitmaps (*.bmp)|*.bmp|" & _
  220.         "GIFs (*.gif)|*.gif|" & _
  221.         "JPEGs (*.jpg)|*.jpg;*.jpeg|" & _
  222.         "Icons (*.ico)|*.ico|" & _
  223.         "Cursors (*.cur)|*.cur|" & _
  224.         "Run-Length Encoded (*.rle)|*.rle|" & _
  225.         "Metafiles (*.wmf)|*.wmf|" & _
  226.         "Enhanced Metafiles (*.emf)|*.emf|" & _
  227.         "Graphic Files|*.bmp;*.gif;*.jpg;*.jpeg;*.ico;*.cur;*.rle;*.wmf;*.emf|" & _
  228.         "All Files (*.*)|*.*"
  229. End Sub
  230. ' Load the indicated file.
  231. Private Sub mnuFileOpen_Click()
  232. Dim file_name As String
  233.     ' Let the user select a file.
  234.     On Error Resume Next
  235.     dlgOpenFile.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
  236.     dlgOpenFile.ShowOpen
  237.     If Err.Number = cdlCancel Then
  238.         Exit Sub
  239.     ElseIf Err.Number <> 0 Then
  240.         Beep
  241.         MsgBox "Error selecting file.", , vbExclamation
  242.         Exit Sub
  243.     End If
  244.     On Error GoTo 0
  245.     Screen.MousePointer = vbHourglass
  246.     DoEvents
  247.     file_name = Trim$(dlgOpenFile.FileName)
  248.     dlgOpenFile.InitDir = Left$(file_name, Len(file_name) _
  249.         - Len(dlgOpenFile.FileTitle) - 1)
  250.     Caption = "BinCont2 [" & dlgOpenFile.FileTitle & "]"
  251.     ' Open the original file.
  252.     On Error GoTo LoadError
  253.     picOriginal.Picture = LoadPicture(file_name)
  254.     On Error GoTo 0
  255.     ' Make picResult the same size and position it.
  256.     ArrangeControls
  257.     ' Make picResult show the same image.
  258.     picResult.Picture = picOriginal.Picture
  259.     DoEvents
  260.     ' Display the brightness histogram.
  261.     ShowHistograms picOriginal
  262.     Screen.MousePointer = vbDefault
  263.     Exit Sub
  264. LoadError:
  265.     Screen.MousePointer = vbDefault
  266.     MsgBox "Error " & Format$(Err.Number) & _
  267.         " opening file '" & file_name & "'" & vbCrLf & _
  268.         Err.Description
  269. End Sub
  270. ' Save the transformed image.
  271. Private Sub mnuFileSaveAs_Click()
  272. Dim file_name As String
  273.     ' Let the user select a file.
  274.     On Error Resume Next
  275.     dlgOpenFile.Flags = cdlOFNOverwritePrompt + cdlOFNHideReadOnly
  276.     dlgOpenFile.ShowSave
  277.     If Err.Number = cdlCancel Then
  278.         Exit Sub
  279.     ElseIf Err.Number <> 0 Then
  280.         Beep
  281.         MsgBox "Error selecting file.", , vbExclamation
  282.         Exit Sub
  283.     End If
  284.     On Error GoTo 0
  285.     Screen.MousePointer = vbHourglass
  286.     DoEvents
  287.     file_name = Trim$(dlgOpenFile.FileName)
  288.     dlgOpenFile.InitDir = Left$(file_name, Len(file_name) _
  289.         - Len(dlgOpenFile.FileTitle) - 1)
  290.     Caption = "BinCont2 [" & dlgOpenFile.FileTitle & "]"
  291.     ' Save the transformed image into the file.
  292.     On Error GoTo SaveError
  293.     SavePicture picResult.Picture, file_name
  294.     On Error GoTo 0
  295.     Screen.MousePointer = vbDefault
  296.     Exit Sub
  297. SaveError:
  298.     Screen.MousePointer = vbDefault
  299.     MsgBox "Error " & Format$(Err.Number) & _
  300.         " saving file '" & file_name & "'" & vbCrLf & _
  301.         Err.Description
  302. End Sub
  303. ' Set the binary contrast enhancement level.
  304. Private Sub picHistogram_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
  305.     If picOriginal.Picture <> 0 Then
  306.         picHistogram(0).Cls
  307.         picHistogram(1).Cls
  308.         picHistogram(2).Cls
  309.         picHistogram(Index).Line _
  310.             (X, picHistogram(Index).ScaleTop)- _
  311.             Step(0, picHistogram(Index).ScaleHeight), vbRed
  312.         Screen.MousePointer = vbHourglass
  313.         DoEvents
  314.         TransformImage Index, X
  315.         Screen.MousePointer = vbDefault
  316.     End If
  317. End Sub
  318.